package cn.neusoft.luofa603.yuejian_29.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.activity.ClubInfoActivity;
import cn.neusoft.luofa603.yuejian_29.bean.Club;
import cn.neusoft.luofa603.yuejian_29.model.JoinModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClubInfoFragment extends Fragment {
    private Club club;
    private TextView introduction,province_name,city_name,district_name,opten_time,equips,club_tel;
    private Button join;
    private String club_id,SessionID,cal;
    private ImageButton c_call;
    private View view;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_clubinfo, container, false);
        ClubInfoActivity activity = (ClubInfoActivity) getActivity();
        club = activity.club;
        initviews();
        innitEvents();
        initData();
        return view;
    }
    public void initviews(){
        join=(Button)view.findViewById(R.id.join);
        c_call=(ImageButton)view.findViewById(R.id.c_call);
        province_name=(TextView)view.findViewById(R.id.province_name);
        city_name=(TextView)view.findViewById(R.id.city_name);
        district_name=(TextView)view.findViewById(R.id.district_name);
        introduction=(TextView)view.findViewById(R.id.introduction);
        opten_time=(TextView)view.findViewById(R.id.open_time);
        equips=(TextView)view.findViewById(R.id.equips);
        club_tel=(TextView)view.findViewById(R.id.club_tel);

    }

    public void innitEvents() {
        join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                joinclub();

            }
        });
       c_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call();

            }
        });
    }

    public void initData() {
        ClubInfo();
        getsessionid();
    }
    public void ClubInfo(){
        province_name.setText(club.getProvince_name());
        city_name.setText(club.getCity_name());
        district_name.setText(club.getDistrict_name());
        introduction.setText(club.getIntroduction());
        opten_time.setText(club.getOpen_time());
        equips.setText(club.getEquipsuggest());
        club_id=club.getId();
        cal=club.getClub_tel();
        if (cal==""){
           club_tel.setText("13972739932");
        }else {
            club_tel.setText(club.getClub_tel());
        }

    }
    public void getsessionid(){
        SharedPreferences sp= getActivity().getSharedPreferences("sessionid", Context.MODE_PRIVATE);
        SessionID=sp.getString("sessionid", null);
        // System.out.println("111111111111111111"+SessionID);
    }
  public void joinclub(){
      JoinModel joinmodel= new JoinModel();
      Call<String> call=joinmodel.joinClub("clubmember",club_id,SessionID);
      call.enqueue(new Callback<String>() {
          @Override
          public void onResponse(Call<String> call, Response<String> response) {
              String result=response.body();
              if(result==null){
                 Toast.makeText(getContext(),"联网失败！",Toast.LENGTH_LONG).show();
              }else{

                  if (result.isEmpty()){

                      Toast.makeText(getContext(),"加入失败！",Toast.LENGTH_LONG).show();
                  }else{

                      Toast.makeText(getContext(),"加入成功！",Toast.LENGTH_LONG).show();

                  }

              }

          }

          @Override
          public void onFailure(Call<String> call, Throwable t) {

          }
      });

  }
    public void Call() {

        Intent intent = new Intent(Intent.ACTION_DIAL);
        if (cal==""){
            intent.setData(Uri.parse("tel:13972739932"));
        }else{
            intent.setData(Uri.parse("tel:"+cal));
        }
        startActivity(intent);


    }

}
